//
//  RentHouseInfoViewController.m
//  Liuhe
//
//  Created by zr on 2018/8/9.
//  Copyright © 2018年 zr. All rights reserved.
//

#import "RentHouseInfoViewController.h"
#import <MessageUI/MessageUI.h>

@interface RentHouseInfoViewController ()<MFMessageComposeViewControllerDelegate>

@property (nonatomic, weak)UIImageView * topImageView;
@property (nonatomic, strong)UILabel * titleLabel;
@property (nonatomic, strong)UILabel * timeLabel;
@property (nonatomic, strong)UILabel * watchLabel;
@property (nonatomic, strong)UILabel * moneyLabel;
@property (nonatomic, strong)UILabel * styleLabel;
@property (nonatomic, strong)UILabel * sizeLabel;
@property (nonatomic, strong)UILabel * categoryLabel;
@property (nonatomic, strong)UILabel * payLabel;
@property (nonatomic, strong)UILabel * updateLabel;
@property (nonatomic, strong)UILabel * floorLabel;
@property (nonatomic, strong)UILabel * adressLabel;
@property (nonatomic, strong)UILabel * localLabel;
@property (nonatomic, strong)UILabel * categoryValueLabel;
@property (nonatomic, strong)UILabel * payValueLabel;
@property (nonatomic, strong)UILabel * updateValueLabel;
@property (nonatomic, strong)UILabel * floorValueLabel;
@property (nonatomic, strong)UILabel * adressValueLabel;
@property (nonatomic, strong)UILabel * localValueLabel;
@property (nonatomic, strong)UILabel * moneyValueLabel;
@property (nonatomic, strong)UILabel * styleValueLabel;
@property (nonatomic, strong)UILabel * sizeValueLabel;
@property (nonatomic, strong)UIView * oneLineView;
@property (nonatomic, strong)UIView * twoLineView;
@property (nonatomic, strong)UIButton * msgButton;
@property (nonatomic, strong)UIButton * callButton;


@end

@implementation RentHouseInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = WHITE_COLOR;
    
    [self setupNav];
    [self setupUI];
    
}


- (void)setupNav{
    
    self.title = @"房屋出租";
    
    UIButton * backButton = [[UIButton alloc] init];
    
    backButton.frame = CGRectMake(0, 0, 12 * SCREENH6, 20 * SCREENH6);
    [backButton setImage:[UIImage imageNamed:@"1返回"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backToView) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    self.navigationItem.leftBarButtonItem = leftButton;
    
}

- (void)backToView{
    
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)setupUI{
    
    UIImageView * topImageView = [UIImageView new];
    
    topImageView.backgroundColor = [UIColor lightGrayColor];
    
    NSArray * arr = self.model.images;
    
    NSURL * url = [NSURL URLWithString:arr.firstObject];
    
    self.topImageView = topImageView;
    
    [self.topImageView sd_setImageWithURL:url];
    
    [self.view addSubview:self.topImageView];
    
    
    UILabel * titleLabel = [UILabel new];
    
    titleLabel.text = self.model.name;
    titleLabel.textColor = BLACK_COLOR;
    titleLabel.font = [UIFont systemFontOfSize:17 * SCREENH6];
    
    self.titleLabel = titleLabel;
    
    [self.view addSubview:self.titleLabel];
    
    // timeStampString 是服务器返回的13位时间戳
    NSString *timeStampString  = self.model.ctime;
    
    // iOS 生成的时间戳是10位
    NSTimeInterval interval    =[timeStampString doubleValue];
    NSDate *date               = [NSDate dateWithTimeIntervalSince1970:interval];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSString *dateString       = [formatter stringFromDate: date];
    NSString * time = [NSString stringWithFormat:@"发布时间：%@",dateString];
    
    self.timeLabel = [self setupLabelWith:time andType:@"normal"];
    [self.view addSubview:self.timeLabel];
    
    self.watchLabel = [self setupLabelWith:@"浏览：840次" andType:@"normal"];
    [self.view addSubview:self.watchLabel];
    
    self.moneyLabel = [self setupLabelWith:@"租金" andType:@"normal"];
    [self.view addSubview:self.moneyLabel];
    
    self.styleLabel = [self setupLabelWith:@"户型" andType:@"normal"];
    [self.view addSubview:self.styleLabel];
    
    self.sizeLabel = [self setupLabelWith:@"面积" andType:@"normal"];
    [self.view addSubview:self.sizeLabel];
    
    self.categoryLabel = [self setupLabelWith:@"类别：" andType:@"normal"];
    [self.view addSubview:self.categoryLabel];
    
    self.payLabel = [self setupLabelWith:@"付款：" andType:@"normal"];
    [self.view addSubview:self.payLabel];
    
    self.updateLabel = [self setupLabelWith:@"装修：" andType:@"normal"];
    [self.view addSubview:self.updateLabel];
    
    self.floorLabel = [self setupLabelWith:@"楼层：" andType:@"normal"];
    [self.view addSubview:self.floorLabel];
    
    self.adressLabel = [self setupLabelWith:@"地址：" andType:@"normal"];
    [self.view addSubview:self.adressLabel];
    
    self.localLabel = [self setupLabelWith:@"小区：" andType:@"normal"];
    [self.view addSubview:self.localLabel];
    
    self.oneLineView = [self setupLineView];
    [self.view addSubview:self.oneLineView];
    
    self.twoLineView = [self setupLineView];
    [self.view addSubview:self.twoLineView];
    
    
    NSString * money = [NSString stringWithFormat:@"%@元/月",self.model.price];
    self.moneyValueLabel = [self setupLabelWith:money andType:@"red"];
    [self.view addSubview:self.moneyValueLabel];

    self.styleValueLabel = [self setupLabelWith:self.model.house andType:@"red"];
    [self.view addSubview:self.styleValueLabel];
    
    NSString * areStr = [NSString stringWithFormat:@"%@㎡",self.model.area];
    self.sizeValueLabel = [self setupLabelWith:areStr andType:@"red"];
    [self.view addSubview:self.sizeValueLabel];
    
    self.categoryValueLabel = [self setupLabelWith:@"整套出" andType:@"black"];
    [self.view addSubview:self.categoryValueLabel];
    
    self.payValueLabel = [self setupLabelWith:@"面议" andType:@"black"];
    [self.view addSubview:self.payValueLabel];
    
    self.updateValueLabel = [self setupLabelWith:self.model.decorate andType:@"black"];
    [self.view addSubview:self.updateValueLabel];
    
    self.floorValueLabel = [self setupLabelWith:@"18楼" andType:@"black"];
    [self.view addSubview:self.floorValueLabel];
    
    self.adressValueLabel = [self setupLabelWith:self.model.address andType:@"black"];
    [self.view addSubview:self.adressValueLabel];
    
    self.localValueLabel = [self setupLabelWith:self.model.name andType:@"black"];
    [self.view addSubview:self.localValueLabel];
    
    
    self.msgButton = [self setupButtonWith:@"wewq3"];
    [self.view addSubview:self.msgButton];
    
    self.callButton = [self setupButtonWith:@"erwrew32"];
    [self.view addSubview:self.callButton];
}


-(void)viewDidLayoutSubviews{
    
    [self.topImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.top.mas_equalTo(self.view);
        make.height.mas_equalTo(202 * SCREENH6);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.view).offset(15 * SCREENH6);
        make.top.mas_equalTo(self.topImageView.mas_bottom).offset(10 * SCREENH6);
    }];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.titleLabel);
        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(10 * SCREENH6);
    }];
    
    [self.watchLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.timeLabel.mas_trailing).offset(20 * SCREENH6);
        make.centerY.mas_equalTo(self.timeLabel);
    }];
    
    [self.oneLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self.view);
        make.height.mas_equalTo(2 * SCREENH6);
        make.top.mas_equalTo(self.timeLabel.mas_bottom).offset(10 * SCREENH6);
    }];
    
    [self.moneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.view).offset(57 * SCREENH6);
        make.top.mas_equalTo(self.oneLineView.mas_bottom).offset(15 * SCREENH6);
    }];
    
    [self.moneyValueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.moneyLabel);
        make.top.mas_equalTo(self.moneyLabel.mas_bottom).offset(15 * SCREENH6);
    }];
    
    [self.styleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.centerY.mas_equalTo(self.moneyLabel);
    }];
    
    [self.styleValueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.styleLabel);
        make.centerY.mas_equalTo(self.moneyValueLabel);
    }];
    
    [self.sizeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.view).offset(-57 * SCREENH6);
        make.centerY.mas_equalTo(self.styleLabel);
    }];
    
    [self.sizeValueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.sizeLabel);
        make.centerY.mas_equalTo(self.styleValueLabel);
    }];
    
    [self.twoLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self.view);
        make.height.mas_equalTo(1 * SCREENH6);
        make.top.mas_equalTo(self.moneyValueLabel.mas_bottom).offset(15 * SCREENH6);
    }];
    
    [self.categoryLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.timeLabel);
        make.top.mas_equalTo(self.twoLineView.mas_bottom).offset(20 * SCREENH6);
    }];

    [self.categoryValueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.categoryLabel.mas_trailing);
        make.centerY.mas_equalTo(self.categoryLabel);
    }];

    [self.updateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.categoryLabel);
        make.top.mas_equalTo(self.categoryLabel.mas_bottom).offset(20 * SCREENH6);
    }];

    [self.updateValueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.updateLabel.mas_trailing);
        make.centerY.mas_equalTo(self.updateLabel);
    }];

    [self.adressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.updateLabel);
        make.top.mas_equalTo(self.updateLabel.mas_bottom).offset(20 * SCREENH6);
    }];

    [self.adressValueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.adressLabel.mas_trailing);
        make.centerY.mas_equalTo(self.adressLabel);
    }];

    [self.localLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.adressLabel);
        make.top.mas_equalTo(self.adressLabel.mas_bottom).offset(20 * SCREENH6);
    }];

    [self.localValueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.localLabel.mas_trailing);
        make.centerY.mas_equalTo(self.localLabel);
    }];

    [self.payLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.view).offset(187 * SCREENH6);
        make.centerY.mas_equalTo(self.categoryLabel);
    }];

    [self.payValueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.payLabel.mas_trailing);
        make.centerY.mas_equalTo(self.payLabel);
    }];

    [self.floorLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.payLabel);
        make.centerY.mas_equalTo(self.updateLabel);
    }];

    [self.floorValueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.floorLabel.mas_trailing);
        make.centerY.mas_equalTo(self.floorLabel);
    }];
    
    [self.msgButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.bottom.mas_equalTo(self.view);
        make.height.mas_equalTo(49 * SCREENH6);
        make.width.mas_equalTo(188 * SCREENH6);
    }];
    
    [self.callButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.height.mas_equalTo(self.msgButton);
        make.trailing.mas_equalTo(self.view);
        make.leading.mas_equalTo(self.msgButton.mas_trailing);
    }];
}


- (UIButton *)setupButtonWith:(NSString *)str{
    
    UIButton * button = [UIButton new];
    
    [button addTarget:self action:@selector(ClickBottomButton:) forControlEvents:UIControlEventTouchUpInside];
    [button setImage:ImageNamed(str) forState:UIControlStateNormal];
    
    
    return button;
}


- (UILabel *)setupLabelWith:(NSString *)string andType:(NSString *)type{
    
    UILabel * label = [UILabel new];
    
    label.text = string;
    
    if ([type isEqualToString:@"red"]) {
        
        label.textColor = [UIColor redColor];
        label.font = [UIFont systemFontOfSize:15 * SCREENH6];
    }else{
        
        if ([type isEqualToString:@"normal"]) {
            
            label.textColor = ColorWithRGB(0xB3B3B3);
        }else if ([type isEqualToString:@"black"]) {
            
            label.textColor = BLACK_COLOR;
        }
        
        label.font = [UIFont systemFontOfSize:13 * SCREENH6];
    }
    
    
    
    
    return label;
}


- (UIView *)setupLineView{
    
    UIView * lineView = [UIView new];
    
    lineView.backgroundColor = ColorWithRGB(0xE6E6E6);
    
    return lineView;
}


- (void)ClickBottomButton:(UIButton *)button{
    
    if (button == self.callButton) {
        
        NSString * tel = [NSString stringWithFormat:@"tel:%@",self.model.phone];
        
        UIWebView * callWebview = [[UIWebView alloc]init];
        
        [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:tel]]];
        
        [[UIApplication sharedApplication].keyWindow addSubview:callWebview];
        
    }else if (button == self.msgButton){
        
        MFMessageComposeViewController *vc = [[MFMessageComposeViewController alloc] init];
        // 设置短信内容
        vc.body = @"";
        
        // 设置收件人列表
        vc.recipients = @[self.model.phone];  // 号码数组
        // 设置代理
        vc.messageComposeDelegate = self;
        // 显示控制器
        [self presentViewController:vc animated:YES completion:nil];
        
    }
    
}


- (void)messageComposeViewController:(MFMessageComposeViewController*)controller didFinishWithResult:(MessageComposeResult)result
{
    // 关闭短信界面
    [controller dismissViewControllerAnimated:YES completion:nil];
    if(result == MessageComposeResultCancelled) {
        NSLog(@"取消发送");
    } else if(result == MessageComposeResultSent) {
        NSLog(@"已经发出");
    } else {
        NSLog(@"发送失败");
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
